package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.config.JwtConfig;
import com.explore.demo.mapper.RecruitmentMapper;
import com.explore.demo.mapper.UserMapper;
import com.explore.demo.pojo.DeliveryLog;
import com.explore.demo.mapper.DeliveryLogMapper;
import com.explore.demo.pojo.User;
import com.explore.demo.service.DeliveryLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.explore.demo.utils.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolainan
 * @since 2021-05-15
 */
@Service
public class DeliveryLogServiceImpl extends ServiceImpl<DeliveryLogMapper, DeliveryLog> implements DeliveryLogService
{

    @Resource
    private JwtConfig jwtConfig;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DeliveryLogMapper deliveryLogMapper;

    @Resource
    private RecruitmentMapper recruitmentMapper;

    @Resource
    private FileUtils fileUtils;

    @Override
    public ServerResponse deliveryResume(HttpServletRequest request, Integer recruitmentId, MultipartFile multipartFile)
    {
        String token =  request.getHeader(jwtConfig.getHeader());
        String phone = jwtConfig.getUsernameFromToken(token);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("account", phone));
        String path = fileUtils.saveFile(multipartFile);
        DeliveryLog deliveryLog = new DeliveryLog();
        deliveryLog.setRecruitId(recruitmentId);
        deliveryLog.setUserId(user.getId());
        deliveryLog.setFilePath(path);
        int res = deliveryLogMapper.insert(deliveryLog);
        return res != 0 ?
               ServerResponse.createBySuccessMessage("success", 200, null):
               ServerResponse.createBySuccessMessage("error", 2000, null);
    }



}
